/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.user.service.impl;

import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.modules.user.domain.ManagementAwardData;
import co.yixiang.modules.user.service.ManagementAwardDataService;
import co.yixiang.modules.user.service.dto.ManagementAwardDataDto;
import co.yixiang.modules.user.service.dto.ManagementAwardDataQueryCriteria;
import co.yixiang.modules.user.service.mapper.ManagementAwardDataMapper;
import lombok.AllArgsConstructor;
import co.yixiang.dozer.service.IGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import co.yixiang.domain.PageResult;
/**
* @author lsh
* @date 2021-05-12
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "managementAwardData")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ManagementAwardDataServiceImpl extends BaseServiceImpl<ManagementAwardDataMapper, ManagementAwardData> implements ManagementAwardDataService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public PageResult<ManagementAwardDataDto> queryAll(ManagementAwardDataQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<ManagementAwardData> page = new PageInfo<>(queryAll(criteria));
        return generator.convertPageInfo(page,ManagementAwardDataDto.class);
    }


    @Override
    //@Cacheable
    public List<ManagementAwardData> queryAll(ManagementAwardDataQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(ManagementAwardData.class, criteria));
    }


    @Override
    public void download(List<ManagementAwardDataDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ManagementAwardDataDto managementAwardData : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("月管理奖基础名称", managementAwardData.getTitle());
            map.put("分成比例", managementAwardData.getShareRatio());
            map.put("金额最大值", managementAwardData.getUseMaxPrice());
            map.put("金额最小值", managementAwardData.getUseMinPrice());
            map.put("有效期限（单位：天）", managementAwardData.getCouponTime());
            map.put("排序", managementAwardData.getSort());
            map.put("状态（0：关闭，1：开启）", managementAwardData.getStatus());
            map.put("类型", managementAwardData.getType());
            map.put("项目添加时间", managementAwardData.getCreateTime());
            map.put(" updateTime",  managementAwardData.getUpdateTime());
            map.put("是否删除", managementAwardData.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
